package com.yycx.module.bus.provider.service.impl;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusSupplier;
import com.yycx.module.bus.client.entity.BusSupplierBank;
import com.yycx.module.bus.provider.mapper.BusSupplierBankMapper;
import com.yycx.module.bus.provider.service.BusSupplierBankService;
import com.yycx.module.bus.provider.service.BusSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 供应商银行账户接口实现类
 *
 * @author flyme
 * @date 2020-08-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusSupplierBankServiceImpl extends BaseServiceImpl<BusSupplierBankMapper, BusSupplierBank> implements BusSupplierBankService {

    @Autowired
    private BusSupplierService supplierService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusSupplierBank bsb, EntityMap extra) {
        bsb.setBankState(CommonConstants.ENABLED);
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusSupplierBank> cq, BusSupplierBank bsb, EntityMap requestMap) {
        Long supplierId = bsb.getSupplierId();
        cq.eq(true, "supplierId", supplierId);
        cq.orderByDesc("bsb.createTime");
        return ResultBody.ok();
    }


    @Override
    public ResultBody beforeListEntityMap(CriteriaQuery<BusSupplierBank> cq, BusSupplierBank supplierBank, EntityMap requestMap) {
        cq.eq(BusSupplierBank.class, "supplierId");
        cq.eq(true, "bankState", CommonConstants.ENABLED);
        cq.eq(BusSupplierBank.class, "bankType");
        return super.beforeListEntityMap(cq, supplierBank, requestMap);
    }


    @Override
    public ResultBody deleteBySupplierId(Serializable supplierId) {
        CriteriaDelete cd = new CriteriaDelete();
        cd.eq(true, "supplierId", supplierId);
        Boolean result = remove(cd);
        return ResultBody.ok("删除成功", result);
    }

    @Override
    public ResultBody getBySupplierIdAndBankType(Long supplierId, Integer bankType) {
        CriteriaQuery cq = new CriteriaQuery(BusSupplierBank.class);
        cq.eq(true, "bankType", bankType);
        cq.eq(true, "bankState", CommonConstants.ENABLED);
        cq.eq(true, "supplierId", supplierId);
        BusSupplierBank supplierBank = getOne(cq, false);
        if (FlymeUtils.isNotEmpty(supplierBank)) {
            BusSupplier supplier = supplierService.getById(supplierId);
            EntityMap map = new EntityMap();
            String linkTel = FlymeUtils.getString(supplierBank.getLinkTel(), supplier.getLinkTel());
            String linkMan = FlymeUtils.getString(supplierBank.getLinkMan(), supplier.getLinkMan());
            map.put("bankName", supplierBank.getBankName());
            map.put("bankNo", supplierBank.getBankNo());
            map.put("linkTel", linkTel);
            map.put("address", supplier.getAddress());
            map.put("linkMan", linkMan);
            map.put("taxNumber", supplier.getTaxNumber());
            map.put("supplierName", supplier.getSupplierName());
            return ResultBody.ok(map);
        } else {
            return ResultBody.failed("供货商开户行未配置,请联系管理员");
        }
    }

    @Override
    public ResultBody getByBankId(Long bankId) {
        CriteriaQuery cq = new CriteriaQuery(BusSupplierBank.class);
        cq.eq(true, "bankId", bankId);
        BusSupplierBank supplierBank = getOne(cq, false);
        if (FlymeUtils.isNotEmpty(supplierBank)) {
            BusSupplier supplier = supplierService.getById(supplierBank.getSupplierId());
            EntityMap map = new EntityMap();
            String linkTel = FlymeUtils.getString(supplierBank.getLinkTel(), supplier.getLinkTel());
            String linkMan = FlymeUtils.getString(supplierBank.getLinkMan(), supplier.getLinkMan());
            map.put("bankName", supplierBank.getBankName());
            map.put("bankNo", supplierBank.getBankNo());
            map.put("linkTel", linkTel);
            map.put("address", supplier.getAddress());
            map.put("linkMan", linkMan);
            map.put("taxNumber", supplier.getTaxNumber());
            map.put("supplierName", supplier.getSupplierName());
            return ResultBody.ok(map);
        } else {
            return ResultBody.failed("供货商开户行未配置,请联系管理员");
        }
    }
}
